<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Home_controller extends CI_Controller {

    function __construct() {
        parent::__construct();

//<editor-fold desc="loads">
        $this->load->helper('url'); //para utilizar base_url
        $this->load->helper('array');
        $this->load->model('home_model');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('table');
        $this->load->library('session'); //exibir msg quando registro é incçuído com sucesso no bd.
//</editor-fold>
//<editor-fold desc="carrega calendário ao carregar o controller principal">
        $prefs = array(
            'show_next_prev' => 'TRUE',
            'next_prev_url' => base_url() . 'home_controller/calendario',
            'day_type' => 'long',
        ); //</editor-fold>
//<editor-fold desc="template prefs">
        $prefs['template'] = '
            {table_open}<table class="calendar">{/table_open}
            {week_day_cell}<th class="day_header">{week_day}</th>{/week_day_cell}
            {cal_cell_content}<span class="day_listing">{day}</span>&nbsp;&bull; {content}&nbsp;{/cal_cell_content}
            {cal_cell_content_today}<div class="today"><span class="day_listing">{day}</span>&bull; {content}</div>{/cal_cell_content_today}
            {cal_cell_no_content}<span class="day_listing">{day}</span>&nbsp;{/cal_cell_no_content}
            {cal_cell_no_content_today}<div class="today"><span class="day_listing">{day}</span></div>{/cal_cell_no_content_today}
        ';
//</editor-fold>
//<editor-fold desc="template prefs2">        
//   $prefs2['template'] = '{table_open}<table class="calendar border="0" cellpadding="0" cellspacing="0">{/table_open}
//
//   {heading_row_start}<tr>{/heading_row_start}
//
//   {heading_previous_cell}<th><a href="{previous_url}">&lt;&lt;</a></th>{/heading_previous_cell}
//   {heading_title_cell}<th colspan="{colspan}">{heading}</th>{/heading_title_cell}
//   {heading_next_cell}<th><a href="{next_url}">&gt;&gt;</a></th>{/heading_next_cell}
//
//   {heading_row_end}</tr>{/heading_row_end}
//
//   {week_row_start}<tr>{/week_row_start}
//   {week_day_cell}<td>{week_day}</td>{/week_day_cell}
//   {week_row_end}</tr>{/week_row_end}
//
//   {cal_row_start}<tr>{/cal_row_start}
//   {cal_cell_start}<td>{/cal_cell_start}
//
//   {cal_cell_content}<a href="{content}">{day}</a>{/cal_cell_content}
//   {cal_cell_content_today}<div class="highlight"><a href="{content}">{day}</a></div>{/cal_cell_content_today}
//
//   {cal_cell_no_content}{day}{/cal_cell_no_content}
//   {cal_cell_no_content_today}<div class="highlight">{day}</div>{/cal_cell_no_content_today}
//
//   {cal_cell_blank}&nbsp;{/cal_cell_blank}
//
//   {cal_cell_end}</td>{/cal_cell_end}
//   {cal_row_end}</tr>{/cal_row_end}
//
//   {table_close}</table>{/table_close}';
//        
//</editor-fold>        
        $this->load->library('calendar', $prefs);
    }

    public function index() {
        $dados = array(
            'tela' => 'calendario_view',
            'info' => array(3 => 'linha 1 do bd',
                7 => 'linha 2 do bd',
                (int) '13' => '<a href="http://uol.com.br" target=blank> link</a>',
                26 => 'teste'
            ),
        );

        $this->load->view('home_view', $dados);
    }

    public function calendario() {
        $dados = array(
            'tela' => 'calendario_view',
            'info' => array(
                3 => 'linha 1 do bd',
                7 => 'linha 2 do bd',
                (int) '13' => '<a href="http://uol.com.br" target=blank> link</a>',
                26 => 'teste',
            ),
        );

        $this->load->view('home_view', $dados);
    }

    public function info_user() {

//<editor-fold desc="dados do usuário">
        $dados = array(
            'tela' => 'formuser_view',
        );

        $info_user = elements(array('nome_user', 'email_user', 'ramal_user', 'tel_user',
            'cel_user', 'vinculo_user', 'tipodoc_user', 'doc_user'), $this->input->post());

        //<editor-fold desc="validação">
        //definindo mensagem de erro
        $this->form_validation->set_message('required', 'Preencha o campo %s.');
        $this->form_validation->set_rules('nome_user', 'NOME', 'trim|required|ucwords|alpha|max_length[200]');
        $this->form_validation->set_rules('email_user', 'EMAIL', 'trim|required|strlower|max_length[50]|valid_email');
        if (!$info_user['ramal_user'] & !$info_user['tel_user'] & !$info_user['cel_user']) {
            $this->form_validation->set_rules('hidden_user', 'RAMAL, TELEFONE ou CELULAR', 'required');
        }
        $this->form_validation->set_rules('ramal_user', 'RAMAL', 'trim|numeric');
        $this->form_validation->set_rules('tel_user', 'TELEFONE', 'trim');
        $this->form_validation->set_rules('cel_user', 'CELULAR', 'trim');
        $this->form_validation->set_rules('vinculo_user', 'VÍNCULO', 'required|strlower');
        $this->form_validation->set_rules('tipodoc_user', 'TIPO DE DOCUMENTO', 'required|strlower');
        $this->form_validation->set_rules('doc_user', 'DOCUMENTO', 'trim|required');
        //</editor-fold>

        if ($this->form_validation->run()) {
            $this->home_model->inserir_user($info_user);
        }

        //carrega home_view
        $this->load->view('home_view', $dados);
    }

    public function select() {
        //              // $this->home_model->select()->result(),
        $dados = array(
            'tela' => 'agendamentos_view',
            'eventos' => $this->home_model->select()->result(),
//            'sel' => $this->home_model->select_nome()->result()
        );
        $this->load->view('home_view', $dados);
    }

    public function update() {
        $dados = array(
            'view' => 'update',
        );
        $this->load->view('home_view', $dados);
    }

    public function delete() {
        $dados = array(
            'view' => 'delete',
        );
        $this->load->view('home_view', $dados);
    }

    public function reserva_equip() {
        $dados = array(
            'tela' => 'equip_view',
        );

        $this->load->view('home_view', $dados);
    }

    public function lista_equip() {
        
        $data_equip = elements(array('data_reserva'), $this->input->post());
        
        $dados = array(
            'tela' => 'listequip_view',
            'lequip' => $this->home_model->listareq()->result(),
            'lista_equip' => $this->home_model->listar_equip($data_equip['data_reserva'])->result(),
        );
 
        $this->load->view('home_view', $dados);
    }

}
